package com.bawei.yangzongcai___chenmei.Service;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bawei.yangzongcai___chenmei.JavaClass.MuiClass;
import com.bawei.yangzongcai___chenmei.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class MyServiceActivity extends AppCompatActivity {
  public static   Verhander verhander;
    private ListView mataList;
    public static SeekBar mataSeek;
    public static List<MuiClass> classList;
    private ImageView mataShangyiqu;
    private ImageView mataBofang;
    private ImageView mataXiayiqu;
    private ImageView mataMoju;
    MyService servicelist;
    int benzi=0;
    public static int haizi;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 100 && grantResults[0] == 0) {

            //----------------------------服务类------------------------------------------------------------------------
            Intent intent = new Intent(MyServiceActivity.this, MyService.class);
            startService(intent);
            ServiceConnection serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    MyService.MyBanker myBanker = (MyService.MyBanker) service;
                    servicelist = myBanker.servicelist();
                }
                @Override
                public void onServiceDisconnected(ComponentName name) {
                }
            };
            bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
            //----------------------------服务类结尾------------------------------------------------------------------------


            ContentResolver contentResolver = getContentResolver();
            Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
            classList = new ArrayList<>();
            while (query.moveToNext())
            {
                String title = query.getString(query.getColumnIndex(MediaStore.Video.Media.TITLE)); //歌名
                String artist = query.getString(query.getColumnIndex(MediaStore.Video.Media.ARTIST)); //歌手
                String duration = query.getString(query.getColumnIndex(MediaStore.Video.Media.DURATION)); //总时长
                String data = query.getString(query.getColumnIndex(MediaStore.Video.Media.DATA)); //地址
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE)); //大小
                String album_id = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); //封面id
                int count = Integer.parseInt(duration);
                if (count > 3000) {
                    String time = count / 1000 / 60 + "分" + count / 1000 % 60 + "秒";
                    Uri uri = Uri.parse("content://media/external/audio/albums/" + album_id);
                    Cursor query1 = contentResolver.query(uri, null, null, null, null);
                    while (query1.moveToNext()) {
                        String album_art = query1.getString(query1.getColumnIndex("album_art"));
                        MuiClass blank = new MuiClass(title, artist, time, data, size, album_art);
                        classList.add(blank);
                    }
                }

            }

            Myyinyuebo mybosshander = new Myyinyuebo();//适配器-------------------
            mataList.setAdapter(mybosshander);

            mataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    benzi=position;
                    servicelist.dongfeng(position);
                }
            });

            mataBofang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (benzi%2!=0)
                    {
                        mataBofang.setImageResource(R.drawable.bo);
                        servicelist.bofang();
                    }
                    else
                    {
                        mataBofang.setImageResource(R.drawable.zuan);
                        servicelist.zanting();
                    }
                    benzi++;
                }
            });

            mataShangyiqu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    servicelist.shangyiqu();
                }
            });

            mataXiayiqu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    servicelist.xiayiqu();
                }
            });

            mataMoju.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PopupWindow popupWindow = new PopupWindow();
                    popupWindow.setHeight(600);
                    popupWindow.setWidth(300);
                    popupWindow.setOutsideTouchable(true);
                    View vieo = LayoutInflater.from(MyServiceActivity.this).inflate(R.layout.pupuwindow, null);
                    popupWindow.setContentView(vieo);
                    popupWindow.showAsDropDown(v, Gravity.CENTER,0,0);

                    vieo.findViewById(R.id.dan).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            haizi=1;
                        }
                    });

                    vieo.findViewById(R.id.shun).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            haizi=2;
                        }
                    });

                    vieo.findViewById(R.id.sui).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            haizi=3;
                        }
                    });


                }
            });


        } else {
            finish();//没授权就关闭
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_service);
        initView();
        //音频授权
        requestPermissions(new String[]{
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.READ_SMS,
                Manifest.permission.CALL_PHONE
        }, 100);



    }

    private void initView() {
        mataList = findViewById(R.id.mata_list);
        mataSeek = findViewById(R.id.mata_seek);
        mataShangyiqu = findViewById(R.id.mata_shangyiqu);
        mataBofang = findViewById(R.id.mata_bofang);
        mataXiayiqu = findViewById(R.id.mata_xiayiqu);
        mataMoju = findViewById(R.id.mata_moju);
    }


    //适配器
    public class Myyinyuebo extends BaseAdapter {


        @Override
        public int getCount() {
            return classList.size();
        }

        @Override
        public Object getItem(int position) {
            return classList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            verhander = new Verhander();
            //判断是否为空
            if (convertView == null) {
                convertView = LayoutInflater.from(MyServiceActivity.this).inflate(R.layout.booooooooo,null);
                verhander.imageView = convertView.findViewById(R.id.zhuanimg);
                verhander.textView1 = convertView.findViewById(R.id.zhuantxt1);
                verhander.textView2 = convertView.findViewById(R.id.zhuantxt2);
                verhander.textView3 = convertView.findViewById(R.id.zhuantxt3);
                convertView.setTag(verhander);
            } else {
                verhander = (Verhander) convertView.getTag();
            }

            //赋值
//            Bitmap bitmap = BitmapFactory.decodeFile(classList.get(position).getAlbum_id());
//            verhander.imageView.setImageBitmap(bitmap);
            Glide.with(MyServiceActivity.this).load(classList.get(position).getAlbum_id()).circleCrop().into(verhander.imageView);
            verhander.textView1.setText("" + classList.get(position).getTitle());
            verhander.textView2.setText("" + classList.get(position).getArtist());
            verhander.textView3.setText("" + classList.get(position).getDuration());


            return convertView;
        }

    }


    public class Verhander {
        ImageView imageView;
        TextView textView1;
        TextView textView2;
        TextView textView3;
    }





}
